package com.example.demo.contorller;

import com.example.demo.Bean.Course;
import com.example.demo.Bean.Result;
import com.example.demo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CourseController {

    @Autowired
    CourseService courseService;

    @RequestMapping("/getAllCourse")
    @CrossOrigin
    public Result getAllCourse(String teacherUid){
        try {
            return courseService.getAllCourse(teacherUid);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/getCourseDetail")
    @CrossOrigin
    public Result getCourseDetail(String courseId){
        try {
            return courseService.getCourseDetail(courseId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/modifyCourseDetail")
    @CrossOrigin
    public Result modifyCourseDetail(Course course){
        try {
            return courseService.modifyCourseDetail(course);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/addCourse")
    @CrossOrigin
    public Result addCourse(Course course,String teacherUid){
        try {
            return courseService.addCourse(course, teacherUid);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/searchCourse")
    @CrossOrigin
    public Result searchCourse(String name, String teacherUid, Integer pageNum, Integer pageSize){
        try {
            return courseService.searchCourse(name, teacherUid, pageNum, pageSize);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/deleteCourse")
    @CrossOrigin
    public Result deleteCourse(String courseId,String teacherUid){
        try {
            return courseService.deleteCourse(courseId, teacherUid);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/getCoursePage")
    @CrossOrigin
    public Result getCoursePage(Integer pageNum,Integer pageSize,String teacherUid){
        try {
            return courseService.getPage(pageNum, pageSize, teacherUid);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @RequestMapping("/getTotalCourseNum")
    @CrossOrigin
    public Result getTotalCourseNum(String teacherUid){
        try {
            return courseService.getTotalCourseNum(teacherUid);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }


}
